<template>
    <div class="history">
        <history-cell v-for="(item, index) in this.list" :key="item[0].id" :list="item" type="1" @click.native="toLink(item[0].url)"></history-cell>
        <get-more @click.native="getList()" :isOver="this.isOver"></get-more>
    </div>
</template>

<script>
    import HistoryCell from '@/components/HistoryCell'
    import GetMore from '@/components/GetMore'
    import {Config} from '@/js/Config'
    export default {
        components: {
            HistoryCell,
            GetMore
        },
        data() {
            return {type: '1', index: 1, isOver: false, list: []}
        },
        mounted() {
            this.setTitle();
            this.hasQuery();
        },
        methods: {
            setTitle() {
                document.title = '浏览记录';
            },
            hasQuery() {
                let openid = sessionStorage.getItem('MeiJingKu_openid');
                if (!openid) {
                    this
                        .$router
                        .push({name: 'user_index'});
                    return false;
                }
                this.getList();
            },
            getList() {
                let openid = sessionStorage.getItem('MeiJingKu_openid');
                if (this.isOver) {
                    return false;
                }
                this
                    .$vux
                    .loading
                    .show({text: 'Loading'});
                this
                    .$http
                    .get(
                        'user/BrowseRecords/v1?openid=' + openid + '&index=' + this.index + '&size=' +
                        Config._pageSize
                    )
                    .then(response => {
                        this
                            .$vux
                            .loading
                            .hide();
                        this.index++;
                        let result = response.data;
                        this.isOver = false;
                        for (let i in result.data) {
                            let item = [{
                                id: result
                                    .data[i]
                                    .id,
                                title: result
                                    .data[i]
                                    .title,
                                desc: result
                                    .data[i]
                                    .message,
                                src: result
                                    .data[i]
                                    .picture,
                                date: result
                                    .data[i]
                                    .time,
                                url: unescape(result
                                    .data[i]
                                    .link)
                            }];
                            this
                                .list
                                .push(item);
                        }
                        if (result.data.length < Config._pageSize) {
                            this.isOver = true;
                        }
                    }, response => {
                        Util.errorWarning(this);
                    });
            },
            toLink(obj) {
                location.href = unescape(obj);
            }
        }
    }
</script>

<style lang="less" scoped="scoped">
    .history {
        margin-top: 2rem;
    }
</style>